package com.fy.market.types.common.ex;

import com.fy.market.types.common.reslut.R;
import com.fy.market.types.common.constant.ResultMessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.fy.market.types.common.reslut.R.no;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BaseException.class)
    public R<String> handleBaseException(BaseException e) {
        log.error("业务异常: {}", e.getMessage());
        log.error("业务异常: {}", e);
        return no(e.getMessage());
    }

    /**
     * 处理其他未知异常
     */
    @ExceptionHandler(Exception.class)
    public R<String> handleException(Exception e) {
        log.error("系统异常: {}", e.getMessage());
        log.error("系统异常", e);
        return no(ResultMessageConstant.SYSTEM_ERROR);
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public R<Void> handleIllegalArgumentException(IllegalArgumentException e) {
        log.warn("业务异常: {}", e.getMessage());
        log.warn("业务异常: {}", e);
        return no(e.getMessage());
    }

}
